package com.trkj.service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.trkj.dao.jpa.IOutDoctorAdviceDao;
import com.trkj.dao.jpa.IOutECDetailDao;
import com.trkj.dao.jpa.IOutErxDao;
import com.trkj.dao.jpa.IOutMPCDetailDao;
import com.trkj.dao.mybatis.OutErxManMapper;
import com.trkj.pojos.Card;
import com.trkj.pojos.HospWorkPatientErx;
import com.trkj.pojos.OutErxChargeDetail;
import com.trkj.pojos.OutMedicalProjectChargeDet;
import com.trkj.pojos.PatientDoctorAdvice;
import com.trkj.vo.OutErxVo;


/**
 * 
 * @author DZK
 * 门诊电子处方业务
 * 
 */
@Service

public class OutErxService {
	@Autowired
	private IOutErxDao outErxDao;
	@Autowired
	private IOutECDetailDao outECDetailDao;
	@Autowired
	private IOutMPCDetailDao outMPCDetailDao;
	@Autowired
	private IOutDoctorAdviceDao outDoctorAdviceDao;
	
	@Autowired
	private OutErxManMapper outErxManMapper;
	/**
	 * 添加门诊电子处方
	 */
	
	
	
	
	public void addOutErx(OutErxVo outErxVo){
		HospWorkPatientErx hospWorkPatientErx = saveHospWorkPatientErx(outErxVo);
		
		saveAdvice(outErxVo, hospWorkPatientErx);
	
		saveOutErxChargeDetail(outErxVo, hospWorkPatientErx);
				
		saveOutMedicalProjectChargeDet(outErxVo, hospWorkPatientErx);
	}

	@Transactional
	public void saveOutMedicalProjectChargeDet(OutErxVo outErxVo, HospWorkPatientErx hospWorkPatientErx) {
		List<OutMedicalProjectChargeDet> outMPChargeDets = 
				Arrays.asList(outErxVo.getOutMPChargeDets());
		if(outMPChargeDets != null){
			for(OutMedicalProjectChargeDet outMPCharge: outMPChargeDets){
				outMPCharge.setHospWorkPatientErx(hospWorkPatientErx);
			}
			outMPCDetailDao.save(outMPChargeDets);
		}
	}

	@Transactional
	public void saveOutErxChargeDetail(OutErxVo outErxVo, HospWorkPatientErx hospWorkPatientErx) {
		List<OutErxChargeDetail> outErxChargeDetails = 
				Arrays.asList(outErxVo.getOutErxChargeDetails());
		//建立关系
				if(outErxChargeDetails != null){
					for(OutErxChargeDetail outErxCharge: outErxChargeDetails){
						outErxCharge.setHospWorkPatientErx(hospWorkPatientErx);
					}
					outECDetailDao.save(outErxChargeDetails);
				}
	}

	@Transactional
	public HospWorkPatientErx saveHospWorkPatientErx(OutErxVo outErxVo) {
		HospWorkPatientErx hospWorkPatientErx = outErxVo.getHospWorkPatientErx();
		outErxDao.save(hospWorkPatientErx);		
			return hospWorkPatientErx;
	}
	

	@Transactional
	public void saveAdvice(OutErxVo outErxVo, HospWorkPatientErx hospWorkPatientErx) {
		PatientDoctorAdvice patientDoctorAdvice = outErxVo.getPatientDoctorAdvice();
		patientDoctorAdvice.setHospWorkPatientErx(hospWorkPatientErx);
		outDoctorAdviceDao.save(patientDoctorAdvice);
	}
	
	/**
	 * 根据门诊ID  模糊分页查询病人 
	 */
	
	@Transactional
	public Map<String,Object> queryOutManByLikeID(String likeID,int page,int rows){
		Page<Map<String,Object>> pageE = PageHelper.startPage(page, rows, true);
		List<Map<String,Object>> list= outErxManMapper.likeFindOutManByOutID(likeID);
		
		Map<String,Object> resultMap = new HashMap<>();
		resultMap.put("rows", list);
		resultMap.put("total", pageE.getTotal());
		return resultMap;
	}
	
	/**
	 * 根据姓名  模糊分页查询病人信息
	 * @param likeName
	 * @param page
	 * @param rows
	 * @return
	 */
	@Transactional
	public Map<String,Object> queryOutManByLikeName(String likeName,int page,int rows){
		Page<Map<String,Object>> pageE = PageHelper.startPage(page, rows, true);
		List<Map<String,Object>> list = outErxManMapper.likeFindOutMenByOutName(likeName);
		
		Map<String,Object> resultMap = new HashMap<>();
		resultMap.put("rows", list);
		resultMap.put("total", pageE.getTotal());
		return resultMap;
		

	}
}
